package com.kaochong.xcx.web.controller;

import com.google.common.collect.Lists;

import com.kaochong.xcx.cache.BannerCache;
import com.kaochong.xcx.cache.FormIdCache;
import com.kaochong.xcx.cache.TokenCache;
import com.kaochong.xcx.cache.UserInfoCache;
import com.kaochong.xcx.enums.ResultEnum;
import com.kaochong.xcx.exception.KaoChongException;
import com.kaochong.xcx.pojo.cache.WordModel;
import com.kaochong.xcx.pojo.vo.FormTemplateVO;
import com.kaochong.xcx.pojo.vo.LearnWordVO;
import com.kaochong.xcx.pojo.vo.ReviewWordVO;
import com.kaochong.xcx.service.PlayGameService;
import com.kaochong.xcx.service.UserMistakesService;
import com.kaochong.xcx.service.UserService;
import com.kaochong.xcx.service.UserStageWordsService;
import com.kaochong.xcx.service.WordBookService;
import com.kaochong.xcx.service.WordService;
import com.kaochong.xcx.utils.AESUtil;
import com.kaochong.xcx.utils.JsonUtil;
import com.kaochong.xcx.utils.response.ResponseUtils;
import com.kaochong.xcx.utils.response.ResultVO;
import com.kaochong.xcx.web.aspect.CurrentUser;
import com.kaochong.xcx.web.controller.base.BasicController;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * @author han
 * @date 2018-05-02 15:26
 */
@RestController
@RequestMapping("/recite")
@Slf4j
public class IndexController extends BasicController {
	
	@Autowired
	UserService userService;
	
	@Autowired
	TokenCache tokenCache;
	
	@Autowired
	PlayGameService playGameService;
	
	@Autowired
	UserInfoCache userInfoCache;
	
	@Autowired
	FormIdCache formIdCache;
	
	@Autowired
	WordService wordService;
	
	@Autowired
	UserStageWordsService userStageWordsService;
	
	@Autowired
	WordBookService wordBookService;
	
	@Autowired
	BannerCache bannerCache;
	
	@Autowired
	UserMistakesService userMistakesService;
	
	/*@GetMapping("/clear/info")
	public ResultVO delete() {
		CurrentUser user = takeUser();
		wordService.deleteUserInfo(user.getId());
		redisUtil.delete("recite.token:" + user.getToken());
		return ResponseUtils.success();
	}*/
	
	/**
	 * 修改用户培养目标
	 * @param target
	 * @return
	 */
	@PostMapping("/target")
	public ResultVO changeTarget(@RequestParam(value = "target") Integer target){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		// 2、修改用户培养目标
		userService.updateUserTarget(user, target);
		return ResponseUtils.success();
	}
	
	/**
	 * 重置学习书
	 * @param target
	 * @return
	 */
	@PostMapping("/reset/target")
	public ResultVO resetTarget(@RequestParam(value = "target") Integer target){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		// 2、修改用户培养目标
		userService.resetUserTarget(user, target);
		return ResponseUtils.success();
	}
	
	@GetMapping("/v1/index")
	public ResultVO v1Index(){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		Map<String, Object> userInfo = userService.getUserInfoV1(user);
		return ResponseUtils.success(userInfo);
	}
	
	@GetMapping("/today/words")
	public ResultVO todayWords(Map<String, Object> map){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		List<WordModel> todayWords = userService.todayWords(user);
		List<Map<String, Object>> result = Lists.newArrayList();
		for (WordModel todayWord : todayWords) {
			Map<String, Object> wordMap = new HashMap<>();
			wordMap.put("id", todayWord.getId());
			wordMap.put("voice", todayWord.getVoice());
			result.add(wordMap);
		}
		map.put("todayWords", result);
		return ResponseUtils.success(map);
	}
	
	/**
	 * 开启下一阶段
	 * @return
	 */
	@GetMapping("/open/next")
	public ResultVO openNext(){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		if (null == user.getTarget()) {
			throw new KaoChongException(ResultEnum.TARGET_UN_CHOOSE_ERROR);
		}
		userService.openNext(user);
		return ResponseUtils.success();
	}
	
	/**
	 * 答题
	 * @param index
	 * @param answerIndex
	 * @return
	 */
	@PostMapping("/answer")
	public ResultVO answer(@RequestParam(value = "index", required = false) Integer index,
	                       @RequestParam(value = "answerIndex", required = false) Integer answerIndex){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		if (null == user.getTarget()) {
			throw new KaoChongException(ResultEnum.TARGET_UN_CHOOSE_ERROR);
		}
		Map<String, Object> result = playGameService.answer(user, index, answerIndex);
		return ResponseUtils.success(result);
	}
	
	/**
	 * 开始答题
	 * @return
	 */
	@GetMapping("/begin")
	public ResultVO begin(){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		if (null == user.getTarget()) {
			throw new KaoChongException(ResultEnum.TARGET_UN_CHOOSE_ERROR);
		}
		Map<String, Object> result = playGameService.begin(user);
		return ResponseUtils.success(result);
	}
	
	/**
	 * 当前时间
	 * @param map
	 * @return
	 */
	@GetMapping("/nowTime")
	public ResultVO nowTime(Map<String, Object> map){
		map.put("nowTime", new Date());
		return ResponseUtils.success(map);
	}
	
	/**
	 * 收集formId，做模板消息通知
	 * @param formIds
	 * @return
	 */
	@PostMapping("/collect")
	public ResultVO collect(String formIds) {
		if (StringUtils.isNotBlank(formIds)) {
			CurrentUser user = takeUser();
			String openId = user.getOpenId();
			List<FormTemplateVO> formTemplateVOS = JsonUtil.listFromJson(formIds, FormTemplateVO.class);
			formIdCache.collect1(openId, formTemplateVOS);
		}
		return ResponseUtils.success();
	}
	
	/**
	 * 获取banner
	 * @param map
	 * @return
	 */
	@GetMapping("/banner")
	public ResultVO banner(Map<String, Object> map,
	                       @RequestParam(value = "target", required = false) String target) {
		map = bannerCache.get(target);
		return ResponseUtils.success(map);
	}
	
	/**
	 * 已学单词
	 * @param stage
	 * @param map
	 * @return
	 */
	@GetMapping("/learn/list")
	public ResultVO learnList(@RequestParam(value = "stage") Integer stage, Map<String, Object> map) {
		CurrentUser user = takeUser();
		List<LearnWordVO> learnWordVOS = userStageWordsService.learnList(user, stage);
		map.put("list", learnWordVOS);
		return ResponseUtils.success(map);
	}
	
	/**
	 * 错词复习
	 * @param stage
	 * @param map
	 * @return
	 */
	@GetMapping("/review/mistakes")
	public ResultVO reviewMistakes(@RequestParam(value = "stage") Integer stage, Map<String, Object> map) {
		CurrentUser user = takeUser();
		List<ReviewWordVO> learnWordVOS = userMistakesService.reviewMistakes(user, stage);
		map.put("list", learnWordVOS);
		return ResponseUtils.success(map);
	}
	
	@GetMapping("/review/mistakes/count")
	public ResultVO reviewMistakesCount(@RequestParam(value = "stage") Integer stage, Map<String, Object> map) {
		CurrentUser user = takeUser();
		Integer count = userMistakesService.reviewMistakesCount(user, stage);
		map.put("mistakesCount", count);
		return ResponseUtils.success(map);
	}
	
	/**
	 * 刷新token
	 * @param token
	 * @return
	 */
	@GetMapping("/refreshToken")
	public ResultVO refushToken(@RequestParam(value = "token") String token, Map<String, Object> map){
		CurrentUser user = takeUser();
		// 替换token策略强制用户重新登录
		if (token.length() == 36) {
			String content = user.getId() + "," + user.getOpenId();
			String newToken = AESUtil.encrypt(content, AESUtil.password);
			
			// 2、登录信息写入redis
			user.setToken(newToken);
			tokenCache.setToken(newToken, user);
			tokenCache.delToken(token);
			map.put("newToken", newToken);
		}
		return ResponseUtils.success(map);
	}
	
	@GetMapping("/study/schedule")
	public ResultVO studySchedule(Map<String, Object> map){
		// 1、获取当前登录用户
		CurrentUser user = takeUser();
		List<Map<String, Object>> result = userService.studySchedule(user.getId());
		map.put("scheduleList", result);
		map.put("target", user.getTarget());
		return ResponseUtils.success(map);
	}
}
